import request from '../../utils/request';
// import amapFile from "../../utils/amap-wx.130"

// pages/schedule/schedule.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime:'',
    endTime:'',
    // longitude: 0, // 当前位置的经度值  
    // latitude: 0, // 当前位置的纬度值  
    // targetLongitude: 0, // 目标位置的经度值  
    // targetLatitude: 0, // 目标位置的纬度值  
    dateString: [],
    coach: [],
    courseL: [],
    cityCourse: '',
    storeName: '',
    cityStoreName: '',
    time: 0,
    flag: true,
    change: 0,
    active: 0,
    isActive: 0,
    week: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    date: [],
    title: [{
      info: "团课"
    }, {
      info: "教练"
    }],
    city: [],
    cityName: '河北省/石家庄市',
    store: [],
    showIndex: null, //打开弹窗的对应下标
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDate()
    this.setDate()
    this.getweek()
    this.getCityAll()
    this.getCitycertain()
    this.courseCertain()
    this.courseList()
    this.coachList()
    // this.distance()
  },
  //...

  // 城市列表
  getCityAll() {
    // let that = this
    request({
      url: '/store/city',
      // method:'GET',
    }).then(res => {
      // console.log(res);
      if (res.code === 200) {
        let data = res.data;
        console.log(data);
        this.setData({
          city: data
        })
        // console.log(this.data.city)
      } else {
        console.log(res.message);
      }
    }).catch(err => {
      console.error(err);
    });
  },
  //查询每个城市的所有门店
  getCitycertain() {
    request({
      url: '/store/name?city=' + this.data.cityName, //发请求带参数
      method: 'GET',
    }).then(res => {
      // console.log(res);
      if (res.code === 200) {
        let result = res.data;
        // console.log(result);
        this.setData({
          store: res.data
        })
      } else {
        console.log(res.message);
      }
    }).catch(err => {
      console.error(err);
    });
  },
  //点击选择city事件
  handleClick(e) {
    // console.log(e);
    this.setData({
      isActive: e.target.dataset.id,
      cityName: e.target.dataset.city
      //e.target代表事件目标，也就是触发事件的HTML元素  dataset是该元素上所有以"data-"开头的自定义属性集合
    })
  },
  //选择城市后点击确认键
  cityCertain(e) {
    this.getCitycertain()
    // this.distance()
    var index = e.currentTarget.dataset.index;
    this.setData({
      showIndex: index == '1',
      flag: false
    })
  },
  //课程列表
  courseList() {
    request({
      url: '/course/list?city=' + this.data.cityCourse + '&storeName=' + this.data.cityStoreName,
      method: 'GET',
    }).then(res => {
      // console.log(res)
      if (res.code === 200) {
        let result = res.data;
        // let courseList = res.data.courseList
        console.log(result);
        this.setData({
          courseL: result,
        })

        // let startTime = res.data.courseList.startTime
        // let endTime = res.data.courseList.endTime
        // console.log(startTime + endTime);
        // 使用split()方法按空格分割字符串，然后取分割后的数组中的第2、3、4个元素（小时、分钟、秒）  
        // let timeStart = startTime.split(" ")[1]; // 获取小时、分钟、秒
        // let timeEnd = endTime.split(" ")[1]; // 获取小时、分钟、秒
        // console.log(timeStart);
        // console.log(timeEnd);
      } else {
        console.log(res.message)
      }
    }).catch(err => {
      console.error(err);
    })
  },
  //教练列表
  coachList() {
    request({
      url: '/coach/list',
      method: 'GET',
    }).then(res => {
      console.log(res);
      if (res.code === 200) {
        // console.log(res.data)
        this.setData({
          coach: res.data
        })
      } else {
        console.log(res.message);
      }

    }).catch(err => {
      console.error(err);
    })
  },
  //点击选择门店store事件
  storeClick(e) {
    this.setData({
      isActive: e.target.dataset.id,
      cityCourse: e.target.dataset.city,
      cityStoreName: e.target.dataset.storename
    })
    // console.log(e)
  },
  //选择门店后点击确认键
  courseCertain(e) {
    this.setData({
      showIndex: null,
      flag: true
    })
    this.courseList()
  },
  //...

  // 日期
  changeAct(e) {
    this.setData({
      active: e.target.dataset.id
    })
    // console.log(e)
  },
  // 团课/教练
  changeTitle(e) {
    this.setData({
      change: e.target.dataset.id
    })
    // console.log(e)
  },
  // 打开弹窗
  openPopup(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      showIndex: index,
      flag: false
    })
  },
  //关闭弹窗
  closePopup() {
    this.setData({
      showIndex: null,
      flag: true
    })
  },


  // 点击跳转课程详情界面
  navigator(e) {
    let id = e.currentTarget.dataset.id
    // console.log(e);
    wx.navigateTo({
      url: '/pages/course-info/course-info?id=' + id
    })
  },
  //点击跳转教练详情界面
  navigatorCoach(e){
    let id = e.currentTarget.dataset.id
    console.log(id);
    wx.navigateTo({
      url: '/pages/coach-info/coach-info?id=' + id,
    })
  },

  getDate(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
    var m = dd.getMonth() + 1; //获取当前月份的日期
    var d = dd.getDate();
    return m + "-" + d;
  },
  getweek(i) {
    let today = new Date();
    let weekdays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    let nextDay = new Date(today.getTime() + 24 * 60 * 60 * 1000 * i);
    return weekdays[nextDay.getDay()]
  },
  setDate() {
    this.setData({
      date: [{
          week: '今天',
          Date: this.getDate(0)
        },
        {
          week: this.getweek(1),
          Date: this.getDate(1)
        },
        {
          week: this.getweek(2),
          Date: this.getDate(2)
        },
        {
          week: this.getweek(3),
          Date: this.getDate(3)
        },
        {
          week: this.getweek(4),
          Date: this.getDate(4)
        },
        {
          week: this.getweek(5),
          Date: this.getDate(5)
        },
        {
          week: this.getweek(6),
          Date: this.getDate(6)
        }
      ]
    })
  },
  // // 计算两个经纬度之间的距离  
  // calculateDistance(lat1, lon1, lat2, lon2) {
  //   this.deg2rad()
  //   const R = 6371; // 地球半径，单位为公里  
  //   const dLat = deg2rad(lat2 - lat1); // 将角度转换为弧度  
  //   const dLon = deg2rad(lon2 - lon1);
  //   const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  //     Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
  //     Math.sin(dLon / 2) * Math.sin(dLon / 2);
  //   const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  //   const distance = R * c; // 计算距离
  //   return distance; // 返回结果，单位为公里  
  // },
  //     // 将角度转换为弧度  
  // deg2rad(deg) {
  //   return deg * (Math.PI / 180);
  // },
  // distance() {
  //   this.calculateDistance()
  //   let latitude = this.data.store[0].latitude
  //   let longitude = this.data.store[0].longitude
  //   console.log(latitude,longitude)
  //   //定义经纬度
  //   const lat1 = 37.984096; // 起点纬度
  //   const lon1 = 114.468831; // 起点经度
  //   const lat2 = latitude; // 终点纬度
  //   const lon2 = longitude; // 终点经度
  //   const distance = calculateDistance(lat1, lon1, lat2, lon2);
  //   console.log(`两地之间的距离为：${distance}公里`);
  // }

})